<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Validate;
use think\Session;
use think\Request;
use think\Paginator;


class Vip extends IndexController
{
//    public function _initialize()
//    {
//        if(empty(Session('id'))
//        {
//            return view('index/login');
//        }
//    }
    public function vipClass()
    {
        return view('vip/vipClass');
    }
    public function makeTip($clid)
    {   

        // dump($clid);die;
        //查询课程名称
        $classname = Db::name('myclass')->field('cname')->where('cid='.$clid)->find();
        // dump($classname);die;
       
       $res = Db::name('together_discus')->order('createtime desc')->limit(6)->select();

        header("Cache-control: private");
        return view('vip/maketip',['res'=>$res,'cname'=>$classname,'clid'=>$clid]);
    }
    public function discusKeyTip($clid)
    {
            // dump(session('web')['cid']);die;
        //将课程id设在中
        
        // dump($clid);die;
        //查询课程名称
        $classname = Db::name('myclass')->field('cname')->where('cid='.$clid)->find();
        //临时使用
        
        //查询发帖主题和发帖人 最后回复
        $tipdata = Db::field('u.nickname,t.keytip,t.createtime,t.discontent,t.id')
                ->name('together_discus')
                ->alias('t')
                ->join('web_users u','t.uid = u.id')
                ->where('clid',$clid)
                ->order('createtime','desc')
                // ->limit(10)
                ->paginate(3);
        
        $data = $tipdata->all();        
             // dump($data);die;
        foreach($data as $k=>$v){
                    // dump($v['id']);die;

             $adddata = Db::field('u.nickname,createtime,pid')
                    ->name('discus_reply')
                    ->alias('d')
                    ->join('web_users u','d.uid = u.id')
                    ->where('pid',$v['id'])
                    ->where('clid',$clid)
                    ->order('createtime desc')
                    // ->group('pid')
                    ->select();
                    // dump($v['id']);die;
            //添加回复数 最后回复人和 回复总数字段
            if ($adddata){
                $data[$k]['backname'] = $adddata[0]['nickname'];
                $data[$k]['lasttime'] = date('Y-m-d H:m:s', $adddata[0]['createtime']);
                $num = Db::name('discus_reply')->where('pid',$v['id'])->count();
                $data[$k]['num'] = $num;
            } else {
                $data[$k]['backname'] = '没有回复';
                $data[$k]['lasttime'] = '';
                $data[$k]['num'] = 0;
            }
            }
           // dump($data);die;

        $page = $tipdata->render();
        // dump($page);die; 

        // dump($data);die;
        header("Cache-control: private");
        
        return view('vip/discuskeytip',['data'=>$data,'clid'=>$clid, 'page'=>$page,'cname'=>$classname]);

    }
    public function newKeyTip()
    {
        $data = input('post.');
        $clid = $data['clid'];
        //查询课程名称
        $classname = Db::name('myclass')->field('cname')->where('cid='.$clid)->find();
        //查询课程名称
        $classname = Db::name('myclass')->field('cname')->where('cid='.$clid)->find();
        $data['createtime'] = time();
        $data['discontent'] = strip_tags($data['discontent']);
        $data['uid'] = session('web')['uid'];
        unset($data['__VIEWSTATE']);
        unset($data['__VIEWSTATEGENERATOR']);

        // dump($data);die; 
        // dump($data);die;
        $data['keytip'] = strip_tags($data['keytip']);
        $data['discontent'] = strip_tags($data['discontent']);
        
        $res = Db::name('together_discus')->insert($data);
        $tipdata = Db::name('together_discus')->where('clid='.$clid)->order('createtime','desc')->limit(10)->select();
        // return view('vip/discusKeyTip',['data'=>$tipdata]);
        return $this->discuskeytip($clid);
        
    }

    public function tipDetail($keyid,$clid)
    {
        // dump($clid);die;
        //查询课程名称
        $classname = Db::name('myclass')->field('cname')->where('cid='.$clid)->find();
        //读取发帖人的内容
        $data = Db::field('u.face,u.nickname,t.keytip,t.discontent,t.createtime')
                ->name('together_discus')
                ->alias('t')
                ->join('web_users u','u.id = t.uid')
                ->where('t.id='.$keyid)
                ->where('clid='.$clid)
                ->find();

         $data['createtime'] = date('Y-m-d H:m:s',$data['createtime']);
         // dump($data);die;        
        //读取回复
        $reply = Db::field('d.id,d.pid,d.uid,d.recontent,d.createtime,d.path')
                ->name('discus_reply')
                ->alias('d')
                // ->join('zzz_web_users w','d.uid = w.id')
                ->where('pid ='.$keyid)
                ->where('clid='.$clid)
                ->select();
        // dump($reply);die;
       
        //将所有回复按pid递归排序
        $freply = $this->getTree($reply);
        // dump($freply);die;
        // dump($freply);die;
        foreach($freply as $k=>$v){
                    $date = date('Y-m-d',$v['createtime']);
                    $datetime =explode('-',$date);
                    $time = $datetime['1'].'月'.$datetime['2'].'日';
                    $sdate = date('H:m',$v['createtime']);
                    $ftime = $date;
                    $freply[$k]['createtime'] = $ftime;
                    $nickname = Db::name('web_users')->where('id='.$v['uid'])->find();
                    $a[]=$v['uid'];

                    $secreply = Db::name('discus_reply')->where('path='.$v['id'])->find();

                    // $a[]=$secreply;
                    //添加信息
                    
                    $freply[$k]['nickname'] = $nickname['nickname'];
                    $freply[$k]['face'] = $nickname['face'];
                   
                    $freply[$k]['becontent'] = $secreply['recontent'];
                    $freply[$k]['fuid'] = $secreply['uid'];
              
             
                if(($k+1) < count($freply) && $v['path'] !=0)
                {
                    $freply[$k]['nextpath'] = $freply[$k+1]['path'];
                }else{
                    if(empty($secreply)){
                        $freply[$k]['nextpath'] = 0;
                    }else{
                         $freply[$k]['nextpath'] = $v['id'];
                    }
                }
                   
        } 
        // dump($a);die;
        // dump($freply);die;
        $uid = session('web')['uid'];
        $nickname = session('web')['nickname'];
        // dump($nickname);die;
        // dump($uid);die;

        // if(isset($secreply)){
        //             return view('vip/discusdetail',['data'=>$data,'freply'=>$freply,'secreply'=>$secreply,'keyid'=>$keyid]);  
        // }
        // print_r($freply);die;
       if($freply){
            return view('vip/discusdetail',['data'=>$data,'freply'=>$freply,'keyid'=>$keyid,'clid'=>$clid,'uid'=>$uid,'nickname'=>$nickname,'cname'=>$classname]); 
        } else {
            return view('vip/discusdetail',['data'=>$data,'keyid'=>$keyid,'uid'=>$uid,'clid'=>$clid,'nickname'=>$nickname,'cname'=>$classname]); 
        }
       
    }
  
    public function contentReply()
    {   


        // return 1111111111111111;
        $data = input('post.');
        // dump($data); 
        //查询课程名称
        $clid = $data['clid'];
        // $classname = Db::name('myclass')->field('cname')->where('cid='.$clid)->find();
        $uid = session('web')['uid'];
        $data['uid'] = $uid;
        $data['createtime'] = time();
        //去除html标签
        $data['recontent'] = strip_tags($data['recontent']);
        // dump($data);die;
        // $data['discontent'] = strip_tags($data['discontent']);
        
        $res = Db::name('discus_reply')->insert($data);
        $keyid = input('post.pid');
        // dump($data);die;
        if($res){
            return 'ok';
        }

        
    }
    function getTree( &$list, $parentId = 0, $level = 0, $nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;' ) {
        static $tree = array();
        foreach( $list as $v ) {
            if( $v['path'] == $parentId ) {
                //说明找到，保存
                $v['nbsp'] = str_repeat( $nbsp, $level );
                $v['level'] = $level;
                $tree[] = $v;
                //继续找
                $this->getTree( $list, $v['id'], $level + 1 );
            }
        }
        return $tree;
    }
   
}
